﻿using System.Diagnostics;
using System.Linq;
using System.Web.DomainServices;
using System;
using System.IO;
using System.Collections.Generic;

namespace Web.Services
{
    public partial class DigitalSignageDomainService
    {
       
        public IQueryable<Room> GetRoomByEvent(int eventId)
        {
            return this.ObjectContext.Room.Where(r => r.EventId == eventId);
        }

        protected override void OnError(DomainServiceErrorInfo errorInfo)
        {
            Debug.WriteLine(errorInfo.Error.Message);
            base.OnError(errorInfo);
        }

        public IQueryable<Slot> GetSlotByEvent(int eventId)
        {
            var result = this.ObjectContext.Slot.Where(s => s.Room.EventId == eventId);
            return result;
        }

        public IQueryable<Slot> GetSlotsByRoom(int roomId)
        {
            var result = this.ObjectContext.Slot
                .Where(s => s.RoomId == roomId || s.RoomId == null);
            return result;
        }

        public IQueryable<AllSlots> GetAllSlotsByRoom(int roomId)
        {
            var result = this.ObjectContext.AllSlots
                .Where(s => s.RoomId == roomId || s.RoomId == null);
            return result;
        }


        public string GetCustomDllPath(string eventName, string roomName)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            Debug.WriteLine(path);
            
            // Event
            string fileName = eventName
                .Replace(" ", "")
                .Replace("-", "")
                .Replace("/", "")
                .Replace("&", "");

            // Room
            fileName = fileName + "." 
                + roomName
                    .Replace(" ", "")
                    .Replace("-", "")
                    .Replace("/", "")
                    .Replace("&", "")
                + ".xap";
            string xapFile = "\\ClientBin\\" + fileName;

            if (File.Exists(path + xapFile))
            {
                return fileName;
            }
            else
            {
                // Event
                fileName = eventName
                    .Replace(" ", "")
                    .Replace("-", "")
                    .Replace("/", "")
                    .Replace("&", "")
                     + ".xap";
                xapFile = "\\ClientBin\\" + fileName;

                if (File.Exists(path + xapFile))
                {
                    return fileName;
                }
            }
            return string.Empty;
        }
    }
}